#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <sys/time.h>
 
#include "data_type.h"
#include "cube.h"
#include "cube_define.h"
#include "cube_record.h"

#include "user_define.h"
#include "node_define.h"

#include "node_conn.h"
// add para lib_include

char Buf[DIGEST_SIZE*4];

int node_conn_init(void * sub_proc, void * para)
{
	int ret;
	// add yorself's module init func here
	RECORD(NODE_DEFINE,CONFIG) * node_config;
	// add yorself's module init func here
	node_config=Dalloc0(sizeof(*node_config),NULL);
	if(node_config==NULL)
		return -ENOMEM;

	Memset(node_config,0,sizeof(*node_config));
	proc_share_data_setpointer(node_config);	


	return 0;
}

int node_init_config()
{
    int ret;
	RECORD(NODE_DEFINE,CONFIG) * node_config;
	node_config=proc_share_data_getpointer();
    ret=proc_share_data_getvalue("proc_name",Buf);
    if(ret<0)
        return ret;
    node_config->node_name=dup_str(Buf,DIGEST_SIZE);
	proc_share_data_setpointer(node_config);

    return 0;
}

int node_conn_start(void * sub_proc, void * para)
{
	int ret;
	void * recv_msg;
	int type;
	int subtype;
	RECORD(NODE_DEFINE,CONFIG) * node_config;

    int status_machine = 0;        //status machine state
                                   // 0 : start
                                   // 1 : init finished
                                   // 2 : connect with center
                                   // 3 : working
                                   // 4 : halt 

    node_init_config();

	// add yorself's module exec func here
	while(1)
	{
	    node_config=proc_share_data_getpointer();
		usleep(time_val.tv_usec);
		ret=ex_module_recvmsg(sub_proc,&recv_msg);
		if(ret<0)
			continue;
		if(recv_msg==NULL)
			continue;
		type=message_get_type(recv_msg);
		subtype=message_get_subtype(recv_msg);
		if(!memdb_find_recordtype(type,subtype))
		{
			printf("message format (%d %d) is not registered!\n",
			message_get_type(recv_msg),message_get_subtype(recv_msg));
			continue;
		}

        //   connect information from control center
		if((type==TYPE(MESSAGE))&&(subtype==SUBTYPE(MESSAGE,CONN_SYNI)))
		{
            node_config->node_state = NODE_CONN;
			ret=proc_node_conn(sub_proc,recv_msg);
		}
		else if((type==TYPE(NODE_DEFINE))&&(subtype==SUBTYPE(NODE_DEFINE,CONFIG)))
		{
			ret=proc_node_setconfig(sub_proc,recv_msg);
		}
	}
	return 0;
}

int proc_node_conn(void * sub_proc,void * recv_msg)
{
	int ret;
    void * new_msg;

	RECORD(NODE_DEFINE,BASIC_INFO) * node_info;
    
    node_info=Talloc0(sizeof(*node_info));
    if(node_info==NULL)
        return -ENOMEM;

    ret = proc_share_data_getvalue("uuid",node_info->node_uuid);
    if(ret<0)
        return ret;

    ret=proc_share_data_getvalue("proc_name",Buf);
    if(ret<0)
        return ret;
    node_info->node_name=dup_str(Buf,DIGEST_SIZE);
	
    node_info->type= NODE;

    new_msg=message_create(TYPE_PAIR(NODE_DEFINE,BASIC_INFO),recv_msg);
    if(new_msg == NULL)
        return -EINVAL;

    message_add_record(new_msg,node_info);

	ret=ex_module_sendmsg(sub_proc,new_msg);

	return ret;
}

int proc_node_setconfig(void * sub_proc,void * recv_msg)
{
	int ret;
    void * new_msg;

	RECORD(NODE_DEFINE,CONFIG) * node_config;
	RECORD(NODE_DEFINE,CONFIG) * input_config;

	node_config=proc_share_data_getpointer();
    
    ret=message_get_record(recv_msg,&input_config,0);
    if(ret<0)
        return ret;

    node_config->origin=input_config->origin;
	proc_share_data_setpointer(node_config);

	ret=ex_module_sendmsg(sub_proc,recv_msg);

	return ret;
}
